package cn.itcast.jk.controller.cargo.contract;

import cn.itcast.jk.domain.ContractProduct;
import cn.itcast.jk.domain.Factory;
import cn.itcast.jk.service.ContractProductService;
import cn.itcast.jk.service.FactoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;
import java.util.HashMap;
import java.util.List;


@Controller
public class ContractProductController {
    @Autowired
    ContractProductService contractProductService;
    @Autowired
    FactoryService factoryService;
    //转向新增页面
    @RequestMapping("/cargo/contractproduct/toinsert.action")
    public String toinsert(String contractId,Model model){
        //准备生产厂家下拉列表
        List<Factory> factoryList = factoryService.getListFactory();
        model.addAttribute("factoryList",factoryList);
        //合同ID
        model.addAttribute("contractId",contractId);
        //合同ID下的货物列表
        Map map = new HashMap();
        map.put("contractId",contractId);
        List<ContractProduct> dataList = contractProductService.find(map);
        model.addAttribute("dataList",dataList);
        return "/cargo/contract/jContractProductCreate.jsp";        //货物新增页面
    }
    //新增保存
    @RequestMapping("/cargo/contractproduct/insert.action")
    public String insert(ContractProduct contractProduct,Model model){
        contractProductService.insert(contractProduct);
        model.addAttribute("contractId",contractProduct.getContractId());
        return "redirect:/cargo/contractproduct/toinsert.action";   //转向货物新增页面
    }
    //转向修改页面
    @RequestMapping("/cargo/contractproduct/toupdate.action")
    public String toupdate(String id,Model model){
        //准备生产厂家下拉列表
        List<Factory> factoryList = factoryService.getListFactory();
        model.addAttribute("factoryList",factoryList);

        ContractProduct contractProduct = contractProductService.get(id);
        model.addAttribute("obj",contractProduct);
        return "/cargo/contract/jContractProductUpdate.jsp";        //货物修改页面
    }
    //修改保存
    @RequestMapping("/cargo/contractproduct/update.action")
    public String update(ContractProduct contractProduct,Model model){
        contractProductService.update(contractProduct);
        model.addAttribute("contractId",contractProduct.getContractId());
        return "redirect:/cargo/contractproduct/toinsert.action";   //转向货物新增页面
    }
    //删除货物
    @RequestMapping("/cargo/contractproduct/delete.action")
    public String delete(String id,Model model,String contractId){
        contractProductService.deleteById(id);
        model.addAttribute("contractId",contractId);
        return "redirect:/cargo/contractproduct/toinsert.action";   //转向货物新增页面
    }
}
